/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author barre
 */
public class RegistroServlet extends HttpServlet {
    private String LOGIN_NAME_ATTRIBUTE = "loginName";

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LoginManager.logout(request);
        response.sendRedirect("/Registro.jsp");
        //this.doPost(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map errors = new HashMap();
        HttpSession session = request.getSession(true);
        String pass1 = (String) request.getParameter("password");
        String pass2 = (String) request.getParameter("password2");
        String user = (String) request.getParameter("loginName");
        
        /*String url = request.getParameter("referer");
        url = url.substring(url.lastIndexOf("/"));
        if(url.length()==0) url = "/ShowLogin.jsp";*/
        String url = "/MainPage.jsp";
        //LoginManager.logout(request);
        //if(!LoginManager.authenticated()){
        if(!LoginManager.authenticated(request)){
            if(pass1.equals(pass2) && (pass1!=null) && (pass2!=null) && (pass1.length()>0) && (pass1.length()>0)){
                Usuarios users = new Usuarios();
                if(!users.registered(user)){
                    users.setUsr(user,pass1);
                    session.setAttribute("authenticated", true);
                }else{
                    errors.put("loginName", "User already registered");
                    if (!errors.isEmpty()) {
                        request.setAttribute("errors", errors);
                        url="/Registro.jsp";
                    }
                }
            }else{
                if ( (user == null) || (user.length() == 0) ) {
                  errors.put("loginName", "User name required");
                }
                if ( (pass1 == null) || (pass2 == null) || (pass1.toString().length() == 0) || (pass2.toString().length() == 0) ) {
                  errors.put("password", "Password required");
                }else if(!pass1.equals(pass2)){
                  errors.put("password", "Password doesn't match");
                }
                if (!errors.isEmpty()) {
                  request.setAttribute("errors", errors);
                }
                url="/Registro.jsp";
            }
            session.setAttribute(LOGIN_NAME_ATTRIBUTE, user);
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
        }else{
            //LoginManager.logout(request);
            response.sendRedirect("/MainPage.jsp");
        }
        
        /*response.setHeader("Pragma", "no-cache");  
        response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");  
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");  
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");*/      
        
        //response.sendRedirect(response.encodeRedirectURL(url));
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
